#ifndef _SI_PSI_PARSE_H
#define _SI_PSI_PARSE_H

#ifdef __cplusplus
extern "C"{
#endif

#ifndef	BOOL
typedef 	int	BOOL;
#endif

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef	NULL
#define	NULL		(void *)0
#endif


#define 	SIPSI_PID_PAT					0x0
#define 	SIPSI_PID_CAT		              	0x1
#define 	SIPSI_PID_NIT					0x10		
#define 	SIPSI_PID_SDTBAT				0x11	
#define 	SIPSI_PID_EIT		              	0x12
#define 	SIPSI_PID_TDTTOT				0x14	

#define 	SIPSI_TID_PAT					0x0		
#define 	SIPSI_TID_CAT					0x1		
#define 	SIPSI_TID_PMT					0x2		
#define 	SIPSI_TID_NIT_ACTUAL			0x40	
#define 	SIPSI_TID_NIT_OTHER			0x41
#define 	SIPSI_TID_SDT_ACTUAL			0x42
#define 	SIPSI_TID_SDT_OTHER			0x46
#define 	SIPSI_TID_BAT					0x4a
#define 	SIPSI_TID_EIT_ACTUAL_PF		0x4E
#define 	SIPSI_TID_EIT_OTHER_PF		0x4F
#define 	SIPSI_TID_TDT					0x70	
#define 	SIPSI_TID_TOT					0x73	

typedef enum 
{
	SIPSI_ES_TYPE_RESERVED		= 0x00,
	SIPSI_ES_TYPE_MPEG1_VIDEO	= 0x01,
	SIPSI_ES_TYPE_MPEG2_VIDEO	= 0x02,
	SIPSI_ES_TYPE_MPEG1_AUDIO	= 0x03,
	SIPSI_ES_TYPE_MPEG2_AUDIO	= 0x04,
	SIPSI_ES_TYPE_PRIVATE_SEC	= 0x05,
	SIPSI_ES_TYPE_PRIVATE_PES	= 0x06,
	SIPSI_ES_TYPE_MHEG			= 0x07,
	SIPSI_ES_TYPE_DSM_CC		= 0x08,
	SIPSI_ES_TYPE_H222			= 0x09,
	SIPSI_ES_TYPE_13818_6_A	= 0x0A,
	SIPSI_ES_TYPE_13818_6_B	= 0x0B,
	SIPSI_ES_TYPE_13818_6_C	= 0x0C,
	SIPSI_ES_TYPE_13818_6_D	= 0x0D,
	SIPSI_ES_TYPE_AUX			= 0x0E,
	SIPSI_ES_TYPE_MPEG4_VIDEO	= 0x1B, 
	SIPSI_ES_TYPE_VIDEO_HEVC	= 0x24, 	//Definition of 0x24 HEVC video MPEG TS stream type
	SIPSI_ES_TYPE_VIDEO_CAVS 	= 0x42, 	//avs
	SIPSI_ATSC_DOLBY_DIGITAL	= 0x81,
	SIPSI_ES_TYPE_DTS_AUDIO 	= 0x82,
	SIPSI_ES_TYPE_AAC        		= 0x11,
	SIPSI_ES_TYPE_AAC1        		= 0x0F,
} SIPSI_ES_T;

typedef enum
{
    	SIPSI_SERVICE_TYPE_RESERVED		= 0x00,
	SIPSI_SERVICE_TYPE_VIDEO			= 1,
	SIPSI_SERVICE_TYPE_AUDIO			= 2,
	SIPSI_SERVICE_TYPE_TELETEXT		= 3,
	SIPSI_SERVICE_TYPE_NVODREF		= 4,
	SIPSI_SERVICE_TYPE_NVODSHIFT		= 5,
	SIPSI_SERVICE_TYPE_MOSAIC		= 6,	
	SIPSI_SERVICE_TYPE_FMRADIO		= 0xA,	 
	SIPSI_SERVICE_TYPE_DATA			= 0x0C,
	SIPSI_SERVICE_TYPE_MHP			= 0x10,
	SIPSI_SERVICE_TYPE_MPEG2_HDTV	= 0x11,
	SIPSI_SERVICE_TYPE_VIDEO_3D		= 0x1C,
	SIPSI_SERVICE_TYPE_ANALOG			= 0x20,//-(ATSC 模拟电视)
	SIPSI_SERVICE_TYPE_ADVANCE_SDTV	= 0x16,
	SIPSI_SERVICE_TYPE_ADVANCE_HDTV	= 0x19,	
	SIPSI_SERVICE_TYPE_ADVANCE_HEVC	= 0x1F,
}SIPSI_SERVICE_T;

#define	SIPSI_DESCTAG_REGISTERATION					(unsigned char)0x05
#define 	SIPSI_DESCTAG_CA								(unsigned char)0x09
#define 	SIPSI_DESCTAG_NETWORK_NAME					(unsigned char)0x40
#define 	SIPSI_DESCTAG_SERVICE_LIST					(unsigned char)0x41
#define 	SIPSI_DESCTAG_STUFFING						(unsigned char)0x42
#define 	SIPSI_DESCTAG_SATELLITE						(unsigned char)0x43
#define 	SIPSI_DESCTAG_CABLE							(unsigned char)0x44
#define 	SIPSI_DESCTAG_VBI_DATA						(unsigned char)0x45
#define 	SIPSI_DESCTAG_VBI_TELETEXT					(unsigned char)0x46
#define 	SIPSI_DESCTAG_BOUQUET_NAME					(unsigned char)0x47
#define 	SIPSI_DESCTAG_SERVICE							(unsigned char)0x48
#define 	SIPSI_DESCTAG_COUNTRY_AVAILABILITY			(unsigned char)0x49
#define 	SIPSI_DESCTAG_LINKAGE							(unsigned char)0x4A 
#define 	SIPSI_DESCTAG_NVOD_REFERENCE				(unsigned char)0x4B
#define 	SIPSI_DESCTAG_TIME_SHIFTED_SERVICE			(unsigned char)0x4C
#define 	SIPSI_DESCTAG_SHORT_EVENT					(unsigned char)0x4D
#define 	SIPSI_DESCTAG_EXTENDED_EVENT				(unsigned char)0x4E
#define 	SIPSI_DESCTAG_TIME_SHIFTED_EVENT			(unsigned char)0x4F
#define 	SIPSI_DESCTAG_COMPONENT						(unsigned char)0x50
#define 	SIPSI_DESCTAG_MOSAIC							(unsigned char)0x51
#define 	SIPSI_DESCTAG_STREAM_IDENTIFIER				(unsigned char)0x52
#define 	SIPSI_DESCTAG_CA_IDENTIFIER					(unsigned char)0x53 
#define 	SIPSI_DESCTAG_CONTENT						(unsigned char)0x54 
#define 	SIPSI_DESCTAG_PARENTAL_RATING				(unsigned char)0x55
#define 	SIPSI_DESCTAG_TELETEXT						(unsigned char)0x56
#define 	SIPSI_DESCTAG_TELEPHONE						(unsigned char)0x57
#define 	SIPSI_DESCTAG_LOCAL_TIME						(unsigned char)0x58
#define 	SIPSI_DESCTAG_SUBTITLING						(unsigned char)0x59 
#define 	SIPSI_DESCTAG_TERRESTRIAL					(unsigned char)0x5A
#define 	SIPSI_DESCTAG_MULTILINGUAL_NETWORK_NAME	(unsigned char)0x5B
#define 	SIPSI_DESCTAG_MULTILINGUAL_BOUQUET_NAME	(unsigned char)0x5C
#define 	SIPSI_DESCTAG_MULTILINGUAL_SERVICE_NAME	(unsigned char)0x5D
#define 	SIPSI_DESCTAG_MULTILINGUAL_COMPONENT		(unsigned char)0x5E
#define 	SIPSI_DESCTAG_PRIVATE_DATA_SPECIFIER		(unsigned char)0x5F
#define 	SIPSI_DESCTAG_SERVICE_MOVE					(unsigned char)0x60 
#define 	SIPSI_DESCTAG_SHORT_SMOOTHING_BUFFER		(unsigned char)0x61
#define 	SIPSI_DESCTAG_FREQUENCY_LIST					(unsigned char)0x62
#define 	SIPSI_DESCTAG_PARTIAL_TRANSPORT				(unsigned char)0x63
#define 	SIPSI_DESCTAG_DATA_BROADCAST_MDESCRIPTOR	(unsigned char)0x64 
#define 	SIPSI_DESCTAG_DATA_BROADCAST_ID			(unsigned char)0x66
#define 	SIPSI_DESCTAG_TRANSPORT_STREAM				(unsigned char)0x67	
#define 	SIPSI_DESCTAG_DSNG							(unsigned char)0x68
#define 	SIPSI_DESCTAG_PDC								(unsigned char)0x69 
#define 	SIPSI_DESCTAG_AC3								(unsigned char)0x6A
#define 	SIPSI_DESCTAG_ANCILLARY_DATA				(unsigned char)0x6B
#define 	SIPSI_DESCTAG_CELL_LIST						(unsigned char)0x6C
#define 	SIPSI_DESCTAG_CELL_FREQUENCY_LINK			(unsigned char)0x6D
#define 	SIPSI_DESCTAG_ANNOUNCEMENT_SUPPORT		(unsigned char)0x6E
#define 	SIPSI_DESCTAG_APPLICATION_SIGNALLING		(unsigned char)0x6F
#define 	SIPSI_DESCTAG_ADAPTATION_FIELD_DATA		(unsigned char)0x70
#define 	SIPSI_DESCTAG_SERVICE_IDENTIFIER				(unsigned char)0x71
#define 	SIPSI_DESCTAG_SERVICE_AVAILABILITY			(unsigned char)0x72
#define 	SIPSI_DESCTAG_SERVICE_LCN					(unsigned char)0x83 //Nanchang:0x82, Chongqing:0xE2, default:0x83

#define MAX_LOGICALCELL_CELLS 32
#define MAX_LOGICALCELL_COUNT 32

typedef struct sipsi_mosaic_logical_cell_s
{
	unsigned char		m_u8LinkType;
	unsigned short	m_u8LinkID1;
	unsigned short	m_u8LinkID2;
	unsigned short	m_u8LinkID3;
	unsigned short	m_u8LinkID4;
	unsigned char		m_u8LogicalCellID;
	unsigned char		m_u8CellPresentationInfo;
	unsigned char		m_au8Cells[MAX_LOGICALCELL_CELLS];
}sipsi_mosaic_logical_cell_t;

typedef struct sipsi_mosaicinfo_s
{
	unsigned char				m_u8MosaicEntryPoint;
	unsigned char				m_u8HorizCellCount;
	unsigned char				m_u8VertCellCount;//3 
	unsigned char				m_u8LogicalCellCount;
	sipsi_mosaic_logical_cell_t	m_astLogicalCells[MAX_LOGICALCELL_COUNT];
}sipsi_mosaicinfo_t;

typedef struct sipsi_dvbservice_s
{
	unsigned short	m_u16ServiceId;
	unsigned char	m_u8ReservedFutureUse;//6 bits;
	unsigned char	m_u8EITScheduleFlag;// 1 bits;
	unsigned char	m_u8EITPresentFollowingFlag;
	unsigned short	m_u8RunningStatus;//3 
	unsigned short	m_u8FreeCAMode;
	unsigned short	m_u16DescriptorLoopLength;//12 bits
	unsigned char*	m_pu8Descriptors;
}sipsi_dvbservice_t;

typedef struct sipsi_program_s
{
	unsigned short m_u16ProgNum;
	unsigned short m_u16PmtPid;//13 bits;
}sipsi_program_t;

typedef struct 
{
	unsigned short	m_u16CaId;
	unsigned short	m_u16Pid;
}sipsi_ecminfo_t,sipsi_emminfo_t;

typedef struct 
{
	unsigned char	m_u8StreamType;
	unsigned short	m_u16ElementaryPid;//13 bits
	unsigned short	m_u16EsInfoLength;//12 bits;
	unsigned char*	m_pu8Descriptors;
}sipsi_prog_element_t;

typedef struct 
{
	unsigned short 	m_u16Pid;
	char			m_as8Language[4];
	unsigned char	m_u8TeletextType;// 5 bits;
	unsigned char	m_u8TeletextMagazineNumber;
	unsigned char	m_u8TeletextPageNumber;
}sipsi_teletextinfo_t;

typedef struct sipsi_dvb_stream_s
{
	unsigned short	m_u16TransportStreamId;
	unsigned short	m_u16OriginalNetworkId;
	unsigned short	m_u16TransportDescriptorsLength;
	unsigned char*	m_pu8Descriptors;
}sipsi_dvb_stream_t;

typedef struct
{
	unsigned int	m_u32Frequency;// 32   khz
	unsigned int	m_u32SymbolRate;// kS/s 
	unsigned int	m_u32Modulation;//
}sipsi_cable_tunerinfo_t;

typedef struct
{
	unsigned short	m_u16EventId;
	unsigned short	m_u16MjdDate;//date of start time
	unsigned int		m_u32UtcTime; //time part of start time
	unsigned int		m_u32Duration;
	unsigned short	m_u32RuningStatus;
	unsigned short	m_u32FreeCAMode;
	unsigned short	m_u16DescriptorLoopLength;//12 bits;
	unsigned char*	m_pu8Descriptors;
}sipsi_dvbevent_t;

typedef struct
{
	unsigned short	m_u16Year;
	unsigned short	m_u16Month;
	unsigned short	m_u16Mday;
	unsigned short	m_u16Wday;
	unsigned char	m_u8Hour;
	unsigned char	m_u8Minute;
	unsigned char	m_u8Second;
}sipsi_systime_t;

typedef struct 
{
	char			m_as8CountryCode[4];
	unsigned char	m_u8CountryRegionId;
	unsigned char	m_u8LocalTimeOffsetPolarity;
	unsigned short	m_u8LocalTimeOffset;
	unsigned short	m_u8TimeOfChangeMJD;
	unsigned short	m_u8NextTimeOffset;
	unsigned int	m_u8TimeOfChangeUTC;	
}sipsi_timeoffsetinfo_t;

extern unsigned char* SiPsiFindDescriptor(unsigned char* pu8DescLoop, unsigned short u16DescLoopLen, unsigned char u8Tag);
extern int SiPsiGetMultiName(unsigned char *pu8Desc, char*ps8Name, unsigned char*pu8Lan);
extern int SiPsiSDTGetServiceName(sipsi_dvbservice_t* pstService, char* ps8ProviderName, unsigned short* pu16ProviderNameLen, char* ps8ServiceName, unsigned short* pu16ServiceNameLen, char* ps8Lan);
extern unsigned short SiPsiSDTGetStreamID(unsigned char* pu8Section);
extern unsigned short SiPsiSDTGetNetID(unsigned char*pu8Section);
extern void SiPsiSDTGetService(unsigned char*pu8Section, sipsi_dvbservice_t* pstService,unsigned int u32Index);
extern unsigned int SiPsiSDTGetServiceCount(unsigned char*pu8Section);
extern int SiPsiSDTGetMosaicInfo(sipsi_dvbservice_t*pstService, sipsi_mosaicinfo_t* pstMosaicInfo, unsigned int* pu32LogicCellCnt);
extern unsigned short SiPsiPATGetStreamID(unsigned char* pu8Section);
extern unsigned short SiPsiPATGetPmtPID(unsigned char*pu8Section,unsigned short u16ServiceId);
extern void SiPsiPATGetProgram(unsigned char*pu8Section, sipsi_program_t*prog, unsigned int u32Index);
extern unsigned int SiPsiPATGetProgramCount(unsigned char*pu8Section);
extern unsigned short SiPsiPMTGetServiceID(unsigned char*pu8Section);
extern int SiPsiPMTGetMosaicInfo(unsigned char*pu8Section, sipsi_mosaicinfo_t* pstMosaicInfo, unsigned int* pu32LogicCellCnt);
extern unsigned int SiPsiPMTGetCAInfoCount(unsigned char*pu8Pointer, unsigned short u16Length);
extern int SiPsiPMTGetCAInfo(unsigned char*pu8Pointer, unsigned short u16Length, unsigned int u32Index, sipsi_ecminfo_t *pstEcmInfo);
extern unsigned short SiPsiPMTGetPCRPID(unsigned char* pu8Section);
extern void SiPsiPMTGetElement(unsigned char*pu8Section, sipsi_prog_element_t *pstElement, unsigned int u32Index);
extern unsigned int SiPsiPMTGetElementCount(unsigned char*pu8Section);
extern unsigned short SiPsiPMTGetProgramInfoLen(unsigned char*pu8Section);
extern unsigned int SiPsiPMTGetTeletextInfo(sipsi_prog_element_t*pstElement, sipsi_teletextinfo_t* pstTeletextInfo);
extern unsigned short SiPsiGetSectionLength(unsigned char*pu8Section);
extern unsigned char  SiPsiGetSectionVersion(unsigned char*pu8Section);
extern unsigned short SiPsiGetExtTableID(unsigned char*pu8Section);
extern unsigned char SiPsiGetTableID(unsigned char*pu8Section);
extern unsigned char SiPsiGetSectionNo(unsigned char*pu8Section);
extern unsigned char SiPsiGetLastSectionNo(unsigned char*pu8Section);
extern unsigned int SiPsiGetSectionCrc(unsigned char*pu8Section);
extern unsigned int SiPsiGetDesciptorCount(unsigned char*pu8Desc, unsigned short u16Deslooplen);
extern unsigned short SiPsiNITGetFirstLoopLen(unsigned char *pu8Section);
extern unsigned short SiPsiNITGetTsLoopLen(unsigned char* pu8Section);
extern unsigned int SiPsiNITGetTSCount(unsigned char* pu8Section);
extern unsigned short SiPsiNITGetNetID(unsigned char*pu8Section);
extern void SiPsiNITGetTsStream(unsigned char* pu8Section, sipsi_dvb_stream_t* pstStream, unsigned int u32Index);
extern int SiPsiNITGetNetworkname(unsigned char* pu8Section, char* ps8Name, char* ps8Lan);
extern int SiPsiNITGetDeliveryTuneInfo(sipsi_dvb_stream_t* pstTs, sipsi_cable_tunerinfo_t* psCableTunerInfo);
extern unsigned short SiPsiBatGetFirstLoopLen(unsigned char *pu8Section);
extern unsigned short SiPsiBatGetTsLoopLen(unsigned char*pu8Section);
extern unsigned int SiPsiBatGetTSCount(unsigned char*pu8Section);
extern void SiPsiBatGetTsStream(unsigned char *pu8Section, sipsi_dvb_stream_t* pstStream, unsigned int u32Index);
extern int SiPsiBATGetBouquetname(unsigned char *pu8Section, char* ps8Name, char* ps8Lan);
extern unsigned short SiPsiBATGetBouquetID(unsigned char *pu8Section);
extern void SiPsiEITGetEventInfo(unsigned char* pu8Buf, sipsi_dvbevent_t* pstEvent);
extern unsigned char* SiPsiEITGetEvent(unsigned char*pu8Section, sipsi_dvbevent_t* pstEvent, unsigned int u32Index);
extern unsigned int SiPsiEITGetEventCount(unsigned char*pu8Section);
extern unsigned char SiPsiEITGetSegmentLastSecNo(unsigned char*pu8Section);
extern unsigned char SiPsiEITGetLastTableID(unsigned char*pu8Section);
extern unsigned short SiPsiEITGetNetID(unsigned char*pu8Section);
extern unsigned short SiPsiEITGetStreamID(unsigned char*pu8Section);
extern unsigned short SiPsiEITGetServiceID(unsigned char*pu8Section);
extern int SiPsiEITGetShortEventName(sipsi_dvbevent_t *pstEvent, char* ps8EventName, char *ps8Text, char* ps8Lan);
extern unsigned char SiPsiEITGetExtendEventName(sipsi_dvbevent_t *pstEvent,  char* ps8EventName, char* ps8Lan);
extern int SiPsiEITGetContentType(sipsi_dvbevent_t* pstEvent, unsigned char* pu8Tp1, unsigned char* pu8Tp2);
extern void SiPsiGetYMDFromMJD(unsigned short u16Mjd,unsigned short* pu16Year,unsigned short* pu16Month,unsigned short* pu16Day);
extern unsigned char SiPsiGetWeekDay(unsigned short u16Mjd);
extern void SiPsiGetHMSFromUTC(int u32Utc, unsigned char* pu8Hour, unsigned char* pu8Min, unsigned char* pu8Sec);
extern void SiPsiTDTTOTGetUTCTime(unsigned char*pu8Section,unsigned short *u16Mjd,unsigned int* pu32Time);
extern void SiPsiConvertToSysTime(sipsi_systime_t* psTime,unsigned short u16Mjd,unsigned int u32Utc);
extern unsigned int SiPsiTOTGetOffsetInfo(unsigned char* pu8Section, sipsi_timeoffsetinfo_t *pstTos);


#ifdef __cplusplus
}
#endif

#endif //_SI_PSI_PARSE_H

